<template>
  <el-dialog title="新增仓库" :visible.sync="show" width="70%" v-if="show">
    <span>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="隶属部门">
          <el-cascader
            :show-all-levels="false"
            :options="tree"
            :props="{ value:'id',label:'name',checkStrictly: true}"
            v-model="itemPid"
            clearable
          />
        </el-form-item>
        <el-form-item label="仓库名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.other.remarks"></el-input>
        </el-form-item>
      </el-form>
    </span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="show = false;send(form)">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ["fshow", "form"],
  watch: {
    fshow(a) {
      this.show = true;
    },
    show(a) {
      this.$emit("change", { show: a });
    },
    $gd() {
      this.setTree();
    },
    form() {
      this.setTree();
    },
    itemPid(a) {
      if (!a.length) return;
      this.form.userType = a[a.length - 1];
    }
  },
  data() {
    return {
      show: false,
      tree: [],
      itemPid: []
    };
  },
  created() {
    this.setTree();
  },
  methods: {
    setTree() {
      if (!this.$gd.userType) return;
      this.tree = this.$gd.userType.arr2tree();
      this.itemPid = this.$gd.userType.getTreeID(this.form.userType);
    },
    send(v) {
      const data = {
        cmd: v.id ? 1 : 2,
        val: {
          token: sessionStorage.token,
          table: "depository"
        }
      };
      let tmp = this.$del(v, "id");
      if (v.id) {
        tmp.where = { id: v.id };
      }
      data.val = Object.assign(data.val, tmp);
      this.$post(data, res => {
        if (res.result) {
          this.$emit("change", 201, res.result, !v.id);
        }
      });
    }
  }
};
</script>

